package com.zh.ssmshirofreemarker.controller;

import com.zh.ssmshirofreemarker.entity.User;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class LoginController {


    @RequestMapping(value="/login",method = RequestMethod.POST)
    public String login(User user){
        System.out.println(user);
        Subject currentUser = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(user.getUsername(),user.getPassword());
        token.setRememberMe(true);
        if(!currentUser.isAuthenticated()) {
            try {
                currentUser.login(token);
            } catch (Exception e) {
                System.out.println("登陆失败：" + e.getMessage());
            }
        }

       return "redirect:/";
    }
    @RequestMapping(value = "/login",method = RequestMethod.GET)
    public String init(){
        return "login";
    }

}
